﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ComputerizedRehabForms
{
    public partial class BLBResultsForm : Form
    {
        private FolderBrowserDialog SaveTo = new FolderBrowserDialog();
        private string saveDirectory;
        private static Bitmap resultImage;
        private static Graphics g;
        private int increment = 1;
        private bool inc_set = true;


        public BLBResultsForm()
        {
            InitializeComponent();            
           
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            /*
             * All of the work for the results page is done in this method
             * First, a copy of each line from the Test page is drawn in the same location as the test line
             * Next, a vertical green line is drawn at the midpoint of each line
             * Finally, a red X is drawn at the first 17 spots the mouse was clicked
             */
            if (DataManager.getFormType() == 3 && inc_set)
            {
                increment++;
                inc_set = false;
            }
            

            base.OnPaint(e);
            Graphics g;
            
            g = e.Graphics;

            Pen myPen = new Pen(Color.Red);
            myPen.Width = 3;

            /* Draw the outer third of every line in RED */

            for (int i = 0; i < BLBTestForm.NUM_LINES; i += increment)
            {

                g.DrawLine(myPen, BLBTestForm.lines[i][0], BLBTestForm.lines[i][2], BLBTestForm.lines[i][1], BLBTestForm.lines[i][2]);
            }

            /* Draw the middle third of every line in YELLOW */

            myPen.Color = System.Drawing.Color.Goldenrod;
            for (int i = 0; i < BLBTestForm.NUM_LINES; i += increment)
            {

                g.DrawLine(myPen, BLBTestForm.lines[i][0] + (BLBTestForm.lines[i][3] / 6), BLBTestForm.lines[i][2], BLBTestForm.lines[i][1] - (BLBTestForm.lines[i][3] / 6), BLBTestForm.lines[i][2]);
            }


            /* Draw the inner third of every line in GREEN */

            myPen.Color = System.Drawing.Color.Green;
            for (int i = 0; i < BLBTestForm.NUM_LINES; i += increment)
            {

                g.DrawLine(myPen, BLBTestForm.lines[i][0] + (BLBTestForm.lines[i][3] / 3), BLBTestForm.lines[i][2], BLBTestForm.lines[i][1] - (BLBTestForm.lines[i][3] / 3), BLBTestForm.lines[i][2]);
            }

            /*Draw all of the midpoint lines, in green*/
            myPen.Color = System.Drawing.Color.Green;
            for (int i = 0; i < BLBTestForm.NUM_LINES; i += increment)
            {
                g.DrawLine(myPen, BLBTestForm.midpoint_x[i], BLBTestForm.midpoint_y[i] +25, BLBTestForm.midpoint_x[i], BLBTestForm.midpoint_y[i] -25);
            }
            
            /*Draw all of the mouse click X's, in black*/
            myPen.Color = System.Drawing.Color.Black;
            for (int i = 0; i < BLBTestForm.NUM_LINES; i ++)
            {
                
                    g.DrawLine(myPen, BLBTestForm.mouse_x[i] - 10, BLBTestForm.mouse_y[i] - 10, BLBTestForm.mouse_x[i] + 10, BLBTestForm.mouse_y[i] + 10);
                    g.DrawLine(myPen, BLBTestForm.mouse_x[i] - 10, BLBTestForm.mouse_y[i] + 10, BLBTestForm.mouse_x[i] + 10, BLBTestForm.mouse_y[i] - 10);

                }

            
        }

        private void BLBResultsForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }
        
        private void save_button_Click_1(object sender, EventArgs e)
        {
            /*
             * User clicks the save results button
             * First, let user select folder to save results to
             * Next, screen cap results page, save as pdf (or jpeg)
             * Next, save txt file with numerical data
             */

            SaveTo.ShowDialog();
            saveDirectory = SaveTo.SelectedPath; // user selects folder to save files to        

            //create image and txt filenames, using default name of unknown_unknown_date
            string imageFileName = saveDirectory + "\\unknown_unknown_" + BLBTestForm.stopTime.ToString("MM-dd-yyyy") + ".jpeg";
            string txtFileName = saveDirectory + "\\unknown_unknown_" + BLBTestForm.stopTime.ToString("MM-dd-yyyy") + ".txt";

            if ((!DataManager.CID.Equals("unknown")) && (!DataManager.MRN.Equals("unknown")))
            {
                //Clinician ID and Patient ID have been submitted by user, file names are set as CID_MRN_date
                imageFileName = saveDirectory + "\\" + DataManager.CID + "_" + DataManager.MRN + "_" + BLBTestForm.stopTime.ToString("MM-dd-yyyy") + ".jpeg";
                txtFileName = saveDirectory + "\\" + DataManager.CID + "_" + DataManager.MRN + "_" + BLBTestForm.stopTime.ToString("MM-dd-yyyy") + ".txt";
            }

            System.Threading.Thread.Sleep(1000);// wait 1 second for dialog box to close before saving

            //screen shot results page
            resultImage = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb);
            g = Graphics.FromImage(resultImage);
            g.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);
            
            resultImage.Save(imageFileName, ImageFormat.Jpeg); //save screenshot to directory as a JPEG

            txtWriter(txtFileName);
            
            DataManager.showIntro();// exit after saving; needs new dialogue box

        }

        private void no_save_button1_Click(object sender, EventArgs e)
        {
            DataManager.showExit();
        }

        private void BLBResultsForm_Load(object sender, EventArgs e)
        {

        }

        private void txtWriter(string path)
        {
            /*
             * This method will create a txt file with the name and directory specified by path
             * It will then write numerical data about the current test to that file
             * Fiel format is as follows:
             * 
             * Screen width in pixels
             * deviation from center of each line, in pixels
             * average deviation in pixels
             * test duration in seconds
             * 
             * NOTE: THIS METHOD IS NAIVE!
             * It assumes that click 1 is on line 1, etc
             * Clicking lines in a different order than top-to-bottom will give false results
             */

            System.IO.StreamWriter writer = new System.IO.StreamWriter(path); // create the file to be written to
            writer.WriteLine(BLBTestForm.screen_width); //write width

            int deviation; // negative numbers to the left of the
            int total = 0; //running total of deviations

            int j = 0;
            for (int i = 0; i < BLBTestForm.NUM_LINES; i += increment)
            {
                deviation = BLBTestForm.mouse_x[j] - BLBTestForm.midpoint_x[i];
                total += deviation;
                writer.WriteLine(deviation); //write deviation of each click
                j++;
            }
            writer.WriteLine(total / BLBTestForm.NUM_LINES); //write average deviation
            TimeSpan elapsed = BLBTestForm.stopTime - BLBTestForm.startTime;
            writer.WriteLine(elapsed.TotalSeconds); //write test duration
            writer.Close();
        }
    }
}
